package com.zlt.controller.recruiter;

import com.zlt.controller.BaseController;
import com.zlt.dao.JobDao;
import com.zlt.dto.JobDTO;
import com.zlt.dto.JobTypeDTO;
import com.zlt.entity.JobType;
import com.zlt.entity.User;
import com.zlt.service.JobService;
import com.zlt.service.impl.JobServiceImpl;
import com.zlt.util.ActiveUserUtil;
import com.zlt.util.ClassUtil;
import com.zlt.vo.ActiveUser;
import com.zlt.vo.JobVo;
import com.zlt.vo.Pager;
import com.zlt.vo.Result;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

@WebServlet("/recruiter/job/*")
public class JobController extends BaseController {
    private JobService jobService =new JobServiceImpl();
    @Override
    protected void defaultMethod(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }

    /**
     * 获取所有岗位类型
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected Result jobType(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JobTypeDTO jobTypeDTO = ClassUtil.resolveRequest(JobTypeDTO.class, req);
        if(jobTypeDTO!=null){ //查询数据
            List<JobType> jobTypes= null;
            try {
                jobTypes = jobService.selectAllType(Integer.parseInt(jobTypeDTO.getParentId()));
                if(!jobTypes.isEmpty()&&jobTypes!=null){
                    return Result.ok("查询成功",jobTypes);
                }else {
                    return Result.error("查询失败");
                }
            } catch (NumberFormatException e) {
                return Result.error("查询失败");
            }

        }else {return Result.error("查询失败");}
    }

    /**
     * 发布岗位
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected Result publishJob(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JobDTO jobDTO = ClassUtil.resolveRequest(JobDTO.class, req);
        if(jobDTO!=null){
            try {
                boolean isSuccessInsert=jobService.insertJob(jobDTO);
                if(isSuccessInsert){
                    return Result.ok("发布成功！");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return Result.error("发布失败");
            }
        }
        return Result.error("发布失败");

    }

    /**
     * 更新岗位信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected Result updateJob(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JobDTO jobDTO = ClassUtil.resolveRequest(JobDTO.class, req);
        boolean isUpdate =jobService.updateJobInfo(jobDTO);
        if(isUpdate){
            return Result.ok("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }

    /**
     * 获取所有岗位
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected Result all(HttpServletRequest req, HttpServletResponse resp)  {
        //需要进行分页操作
        JobDTO jobDTO = ClassUtil.resolveRequest(JobDTO.class, req);

        ActiveUser activeUser =  ActiveUserUtil.get();
        User user=new User();
//        try {
////            BeanUtils.copyProperties(user,activeUser);
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (InvocationTargetException e) {
//            e.printStackTrace();
//        }
        //默认是第一页，每页显示101条信息
        if(jobDTO==null){
            jobDTO.setPage(1);
            jobDTO.setLimit(5l);
        }
        Pager<JobVo> pager=new Pager<>();
        pager.setPage(jobDTO.getPage());
        pager.setLimit(jobDTO.getLimit());
        try {
            BeanUtils.copyProperties(user,activeUser);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        try {
            pager= jobService.selectAllJob(pager,user.getUid());
            return Result.ok("分页成功",pager);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("分页失败！");
        }


    }
    /**
     * 下架岗位
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected Result delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //
        String jid = req.getParameter("jid");
        if(jid!=null){
            try {
                boolean isDown = jobService.downJob(Integer.parseInt(jid), 1);
                if(isDown){
                    return Result.ok("下架成功！");
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
                return Result.error("下架失败！");

            }
        }
        return Result.error("下架失败！");
    }
    /**
     * 上架岗位
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected Result put(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //
        String jid = req.getParameter("jid");
        if(jid!=null){
            try {
                boolean isDown = jobService.downJob(Integer.parseInt(jid), 0);
                if(isDown){
                    return Result.ok("上架成功！");
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
                return Result.error("上架失败！");

            }
        }
        return Result.error("上架失败！");
    }

    /**
     * 选择单个岗位信息
     * @param req
     * @param resp
     * @return
     */
    protected Result selectOneJob(HttpServletRequest req, HttpServletResponse resp){
        String jid = req.getParameter("jid");
        JobVo jobVo = null;
        try {
            jobVo = jobService.selectOneJobByJid(Integer.parseInt(jid));
            if(jobVo!=null){
                return Result.ok("查询成功",jobVo);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return Result.error("查询失败！");
        }
        return Result.error("查询失败！");

    }

    /**
     * 删除单个信息，不在显示
     * @param req
     * @param resp
     * @return
     */
    protected Result deleteOne(HttpServletRequest req, HttpServletResponse resp){
        String jid = req.getParameter("jid");
        try {
            boolean isDelete = jobService.downJob(Integer.parseInt(jid), 2);
            if(isDelete){
                return Result.ok("删除成功！");
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }

        return Result.error("删除失败");
    }
}
